package com.chexiehui.jc.utils;

import android.annotation.SuppressLint;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class AppCommonUtil {

    public static String friendlyTime(String sdate) {
        SimpleDateFormat dateFormater2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormater3 = new SimpleDateFormat("HH:mm");
        Date time = null;
        try {
            time = dateFormater2.parse(sdate);
        } catch (ParseException e) {
            return "";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();
        //判断是否是同一天
        String curDate = dateFormater2.format(cal.getTime());
        String paramDate = dateFormater2.format(time);
        if(curDate.equals(paramDate)){
            int hour = (int)((cal.getTimeInMillis() - time.getTime())/3600000);
            if(hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000,1)+"分钟前";
            else
                ftime = hour+"小时前";
            return ftime;
        }
        long lt = time.getTime()/86400000;
        long ct = cal.getTimeInMillis()/86400000;
        int days = (int)(ct - lt);
        if(days == 0){
            int hour = (int)((cal.getTimeInMillis() - time.getTime())/3600000);
            if(hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000,1)+"分钟前";
            else
                ftime = hour+"小时前";
        }
        else if(days == 1){
            ftime = "昨天 " + dateFormater3.format(time);
        }
        else if(days == 2){
            ftime = "前天 ";
        }
        else if(days > 2 && days <= 10){
            ftime = days+"天前";
        }
        else if(days > 10){
            ftime = dateFormater2.format(time);
        }
        return ftime;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getFormatTime(String time) {
        if(StringUtils.isTrimEmpty(time)){
            return "";
        }
        Date d = TimeUtils.string2Date(time, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        if(d == null){
            return "";
        }
        return TimeUtils.date2String(d,new SimpleDateFormat("yyyy-MM-dd"));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getFormatTime(String time, String format) {
        if(StringUtils.isTrimEmpty(time)){
            return "";
        }
        Date d = TimeUtils.string2Date(time, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        if(d == null){
            return "";
        }
        return TimeUtils.date2String(d,new SimpleDateFormat(format));
    }

    public static List<String> stringToSplitList(String targetStr){
        ArrayList<String> list = new ArrayList<>();
        if (targetStr == null || targetStr.trim().equals("")) {
            return list;
        }
        String[] arr = targetStr.contains(",") ? targetStr.split("\\,") : new String[]{targetStr};
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && !arr[i].trim().equals("")) {
                list.add(arr[i]);
            }

        }
        return list;
    }

    public static String stringListToSplitString(List<String> list){
        return stringListToSplitString(list, ",");
    }

    public static String stringListToSplitString(List<String> list, String split){
        if(list == null || list.size() < 1){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if(i == list.size() -1){
                sb.append(list.get(i));
            }else{
                sb.append(list.get(i)).append(split);
            }
        }
        return sb.toString();
    }

    /**
     * 用户电话号码的打码隐藏加星号加*
     *
     * @return 处理完成的身份证
     */
    public static String phoneMask(String phone) {
        String res = "";
        if (!StringUtils.isEmpty(phone)) {
            StringBuilder stringBuilder = new StringBuilder(phone);
            res = stringBuilder.replace(3, 7, "****").toString();
        }
        return res;
    }

    public static boolean isVideo(String url){
        String reg = "(\\.mp4|\\.flv|\\.avi|\\.rm|\\.rmvb|\\.wmv)";
        Pattern p = Pattern.compile(reg);
        return p.matcher(url).find();
    }

}
